package com.hlf.modules.merchant.goods.service.impl;

import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.Query;
import com.hlf.modules.merchant.goods.entity.Goods;
import com.hlf.modules.merchant.goods.entity.GoodsType;
import com.hlf.modules.merchant.goods.mapper.GoodsMapper;
import com.hlf.modules.merchant.goods.mapper.GoodsTypeMapper;
import com.hlf.modules.merchant.goods.service.GoodsTypeService;
import com.hlf.modules.sys.entity.File;
import com.hlf.modules.sys.mapper.FileMapper;
import com.hlf.modules.sys.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("goodsTypeService")
public class GoodsTypeServiceImpl implements GoodsTypeService {
	@Autowired
	private GoodsTypeMapper goodsTypeMapper;
	@Autowired
	private FileMapper fileMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private FileService fileService;

	@Override
	public GoodsType queryObject(Long id) {
		return goodsTypeMapper.queryObject(id);
	}
	@Override
	public int queryTotal(Map<String, Object> map) {
		return goodsTypeMapper.queryTotal(map);
	}
	@Override
	public void save(GoodsType goodsType) {
		goodsTypeMapper.save(goodsType);
	}

	@Override
	public List<GoodsType> batchQueryListStoreId(Long storeId) {
		List<GoodsType> goodsTypes = goodsTypeMapper.queryListForStoreId(String.valueOf(storeId));
		for (GoodsType goodsType : goodsTypes) {
			List<Goods> goodsList = goodsMapper.queryListForGoodsTypId(goodsType.getId());
			for (Goods goods : goodsList) {
				Long id = goods.getId();
				List<File> fileList = fileService.selectByForeignId(id);
				goods.setGoodsImgs(fileList);
			}
			goodsType.setGoodsList(goodsList);
		}
		return goodsTypes;
	}

	/**
	 * 根据主键选择性修改
	 * @param goodsType
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(GoodsType goodsType) {
		return goodsTypeMapper.updateByPrimaryKeySelective(goodsType);
	}

	@Override
	public void delete(Long id) {
		goodsTypeMapper.delete(id);
	}
	@Override
	public void deleteBatch(Long[] ids) {
		goodsTypeMapper.deleteBatch(ids);
	}
	/**
	 * 分页查询
	 */
	@Override
	public PageUtils queryListForPage(Map<String, Object> map) {
		Query query = new Query(map);
		int total = goodsTypeMapper.queryTotal(query);
		List<GoodsType> goodsTypeList=null;
		if(total>0) {
			goodsTypeList=  goodsTypeMapper.queryList(query);
		}
		return new PageUtils(goodsTypeList, total, query.getLimit(), query.getPage());
	}

	/**
	 * 根据商铺id查询,商品类型集合
	 * @param storeId
	 * @return
	 */
	@Override
	public List<GoodsType> queryListForStoreId(String storeId) {
		return goodsTypeMapper.queryListForStoreId(storeId);
	}

	/**
	 * 根据多个ids查询
	 * @param goodsTypeIdList
	 * @return
	 */
	@Override
	public List<GoodsType> queryListForIds(List<Long> goodsTypeIdList) {
		return goodsTypeMapper.queryListForIds(goodsTypeIdList);
	}

	/**
	 * 批量逻辑删除
	 * @param goodsTypeIdList
	 * @return
	 */
	@Override
	public int batchUpdateDel(List<Long> goodsTypeIdList, Long userId){
		return goodsTypeMapper.batchUpdateDel(goodsTypeIdList,userId);
	}

	/**
	 * 选择性新增
	 * @param goodsType
	 * @return
	 */
	@Override
	public int insertSelective(GoodsType goodsType) {
		return goodsTypeMapper.insertSelective(goodsType);
	}


}
